package com.todo.content.application.service.impl;

import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.content.application.convert.ContentConvert;
import com.todo.content.application.entity.bo.ContentTopicRelationBO;
import com.todo.content.application.entity.condition.TopicRelationConditionBO;
import com.todo.content.application.service.ContentTopicRelationService;
import com.todo.content.infrastructure.entity.condition.TopicRelationCondition;
import com.todo.content.infrastructure.entity.po.ContentTopicRelationPO;
import com.todo.content.infrastructure.repository.ContentTopicRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (ContentTopicRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:46
 */
@Slf4j
@Service("contentTopicRelationService")
public class ContentTopicRelationServiceImpl implements ContentTopicRelationService {

    @Resource
    private ContentTopicRelationRepository relationRepository;

    @Override
    public Boolean insertTopicRelationList(List<ContentTopicRelationBO> contentTopicRelationBOList) {
        if (CollectionUtils.isEmpty(contentTopicRelationBOList)) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<ContentTopicRelationPO> collect = contentTopicRelationBOList
                .stream()
                .map(ContentConvert::convertToContentTopicRelationPO)
                .collect(Collectors.toList());
        return relationRepository.batchInsertRelation(collect);

    }

    @Override
    public Boolean removeTopicRelationById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentTopicRelationPO contentTopicRelationPO = relationRepository.queryContentTopicRelationPOById(id);
        if (contentTopicRelationPO == null) {
            return true;
        }
        return relationRepository.removeContentTopicRelationPOById(id);
    }

    @Override
    public Boolean removeTopicRelationByContentId(Long contentId) {
        if (contentId == null || contentId < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<ContentTopicRelationBO> contentTopicRelationBOList = queryTopicRelationByContentId(contentId);
        if (CollectionUtils.isEmpty(contentTopicRelationBOList)) {
            return true;
        }
        return relationRepository.removeTopicRelationByContentId(contentId);
    }

    @Override
    public List<ContentTopicRelationBO> queryTopicRelationByContentId(Long contentId) {
        if (contentId == null || contentId < 0) {
            return Lists.newArrayList();
        }
        List<ContentTopicRelationPO> contentTopicRelationPOS = relationRepository.queryContentTopicRelationPOByContentId(contentId);
        if (contentTopicRelationPOS == null) {
            return Collections.emptyList();
        }
        return contentTopicRelationPOS.stream().map(ContentConvert::convertToContentTopicRelationBO).collect(Collectors.toList());
    }

    @Override
    public PageResult<ContentTopicRelationBO> queryTopicRelationByPage(TopicRelationConditionBO condition) {
        if (condition == null || condition.getTopicCode() == null) {
            return new PageResult<>();
        }
        TopicRelationCondition RelationCondition = ContentConvert.convertToContentTopicRelationCondition(condition);
        PageResult<ContentTopicRelationPO> relationPOPageResult = relationRepository.queryTopicRelationByPage(RelationCondition);
        if (relationPOPageResult == null) {
            return PageResult.of(Lists.newArrayList(), condition.getPageSize(), condition.getPageIndex(), 0L);
        }
        PageResult<ContentTopicRelationBO> result = new PageResult<>();
        result.setResults(relationPOPageResult.getResults() == null ? null : relationPOPageResult.getResults().stream().map(ContentConvert::convertToContentTopicRelationBO).collect(Collectors.toList()));
        result.setPageSize(relationPOPageResult.getPageSize());
        result.setPageCount(relationPOPageResult.getPageCount());
        result.setPageNum(relationPOPageResult.getPageNum());
        result.setTotal(relationPOPageResult.getTotal());
        result.setCurrentIndex(relationPOPageResult.getCurrentIndex());
        result.setHasNext(relationPOPageResult.isHasNext());
        return result;
    }
}
